home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / gadlife / source (ugly) / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-14  |  11.4 KB  |  485 lines  |  [TEXT/KAHL]

  1. /*****************************************************************
  2. *                                                                            *
  3. *    Life 0.4 © Copyright 1989 by Garth Dickie.                                        *
  4. *                                                                            *
  5. *    You can do anything non-commercial with this you like.  This includes using the source and all    *
  6. *    that stuff.  If you want to make money off of something that uses this, check with me first.    *
  7. *                                                                            *
  8. *    I can be reached at 'garth@caen.engin.umich.edu' or 'Garth_Dickie@ub.cc.umich.edu'            *
  9. *                                                                            *
  10. *****************************************************************/
  11.  
  12. #include "main.h"
  13. #include "menus.h"
  14. #include "cursors.h"
  15. #include "patch.h"
  16.  
  17. extern long        doGrowWindow();
  18. extern pascal        doErrSound();
  19.                 
  20. int                isBackground = 0;
  21.  
  22. static int            inBack = 0;
  23.  
  24. main()
  25.     {
  26.     int            waitNext;
  27.     
  28.     MoreMasters();
  29.     MaxApplZone();
  30.     InitGraf(&thePort);
  31.     InitFonts();
  32.     InitWindows();
  33.     InitMenus();
  34.     TEInit();
  35.     InitDialogs( NULL );
  36.     ErrorSound( doErrSound );
  37.     InitCursor();
  38.     
  39.     initDialogs();
  40.     getMenus();
  41.     initScrap();
  42.     
  43.     waitNext = implemented( WNETrapNum );
  44.     installFileTraps();
  45.     doFile( fmNew );
  46.     while( mainEvent( waitNext ));
  47.     removeFileTraps();
  48.     }
  49.  
  50. int mainEvent( waitNext )
  51.     int                waitNext;
  52. {
  53.     static RgnHandle    mouseGood = NULL;
  54.     EventRecord        theEvent;
  55.     int                result = -1, wasEvt;
  56.     long                toWait;
  57.  
  58.     if( !mouseGood ) mouseGood = NewRgn();
  59.     if( !inBack ) updateMouse( mouseGood );
  60.     if( waitNext ) {
  61.         toWait = isBackground ? 0 : waitTime;
  62.         wasEvt = WaitNextEvent( everyEvent, &theEvent, toWait, inBack ? NULL : mouseGood );
  63.     } else {
  64.         SystemTask();
  65.         wasEvt = GetNextEvent( everyEvent, &theEvent );
  66.     }
  67.     if( wasEvt ) result = doEvent( &theEvent );
  68.     else    doBackground( inBack ? lotsElseToDo : nothingElseToDo, noGray, NULL );
  69.     if( !result ) DisposeRgn( mouseGood );
  70.     return( result );
  71. }
  72.     
  73. int doEvent( theEvent )
  74.     EventRecord    *theEvent;
  75. {
  76.     WindowPtr    whichWindow;
  77.     char            theChar;
  78.     int            result = -1;
  79.  
  80.     switch (theEvent->what) {
  81.         case mouseDown:
  82.             return( doMouse( theEvent ));
  83.             break;
  84.         case keyDown:
  85.         case autoKey:
  86.             theChar = theEvent->message & charCodeMask;
  87.             if ( theEvent->modifiers & cmdKey ) {
  88.                 updateMenus();
  89.                 result = doCommand( MenuKey( theChar ));
  90.             } else if( theChar == DEL )
  91.                 doEdit( edClear );
  92.             break;
  93.         case updateEvt:
  94.             doUpdate( theEvent->message );
  95.             break;
  96.         case activateEvt:
  97.             if( theEvent->modifiers & activeFlag )
  98.                 doActivateEvt( theEvent->message );
  99.             else doDeactivateEvt( theEvent->message );
  100.             break;
  101.         case app4Evt: 
  102.             doApp4Evt( theEvent );
  103.             break;
  104.         default: ;
  105.     }
  106.     return( result );
  107. }
  108.  
  109. int doMouse( theEvent )
  110.     EventRecord    *theEvent;
  111.     {
  112.     Rect            sizeRect;
  113.     WindowPtr    whichWindow;
  114.     long            grown, command;
  115.     int            result = -1;
  116.     
  117.     switch ( FindWindow( theEvent->where, &whichWindow ))
  118.         {
  119.         case inGoAway:
  120.             if( doTrackGoAway( whichWindow, theEvent->where ))
  121.                 doFile( fmClose );
  122.             break;
  123.         case inMenuBar:
  124.             updateMenus();
  125.             installMenuTrap();
  126.             command = MenuSelect( theEvent->where );
  127.             removeMenuTrap();
  128.             result = doCommand( command );
  129.             break;
  130.         case inDrag:
  131.             doDragWindow( whichWindow, theEvent->where, NULL );
  132.             if((( WindowPeek )whichWindow )->windowKind >= userKind )
  133.                 doShift( whichWindow );
  134.             break;
  135.         case inGrow:
  136.             setSizeRect( &sizeRect, whichWindow );
  137.             grown = doGrowWindow( whichWindow, theEvent->where, &sizeRect );
  138.             if( grown ) doSizeWindow( whichWindow, LoWord( grown ), HiWord( grown ));
  139.             break;
  140.         case inContent:
  141.             if (FrontWindow() == whichWindow) doContent( whichWindow, theEvent );
  142.             else doChangeTo( whichWindow );
  143.             break;
  144.         case inSysWindow:
  145.             doSysWindow( theEvent, whichWindow );
  146.             break;
  147.         case inDesk:
  148.         default: ;
  149.         }
  150.     return( result );
  151.     }
  152.  
  153. doApp4Evt( theEvent )
  154.     EventRecord    *theEvent;
  155.     {
  156.     int            what;
  157.     
  158.     what = HiWord( theEvent->message ) / 256;
  159.     switch( what )
  160.         {
  161.         case srEvt:
  162.             if( theEvent->message & resumeFlag )
  163.                 doResume( theEvent );
  164.             else    doSuspend( theEvent );
  165.             break;
  166.         default: ;
  167.         }
  168.     }
  169.  
  170. /*******************************************************
  171. *                                                                *
  172. *                                                                *
  173. *******************************************************/
  174.  
  175. WindowPtr frontUserWindow()
  176.     {
  177.     WindowPeek    theWindow;
  178.     
  179.     theWindow = (WindowPeek)FrontWindow();
  180.     while( theWindow && theWindow->windowKind < userKind )
  181.         theWindow = theWindow->nextWindow;
  182.     return( (WindowPtr)theWindow );
  183.     }
  184.  
  185. /*******************************************************
  186. *                                                                *
  187. *   These take the basic actions dictated by events and mouse actions.  SizeWindow    *
  188. *   should redraw any scrollbars, reallocate buffers, and invalidate regions.        *
  189. *   Content should handle mouse actions, including drags and double clicks.  Update    *
  190. *   should handle any/all redrawing of window contents, including the scrollbars and    *
  191. *   growBox, and preferably do some of its own clipping rather than depending on     *
  192. *   the clipRgn to do everything for it.  ChangeTo should at the very least select the    *
  193. *   window and set the port, and change any 'current' values.                    *
  194. *                                                                *
  195. *******************************************************/
  196.  
  197. doSizeWindow( theWindow, w, h )
  198.     WindowPtr    theWindow;
  199.     int            w,h;
  200.     {
  201.     SizeWindow( theWindow, w, h, -1 );
  202.     ClipRect( &( theWindow->portRect ));
  203.     sizeData( GetWRefCon( theWindow ));
  204.     }
  205.  
  206. doShift( theWindow )
  207.     WindowPtr    theWindow;
  208.     {
  209.     shiftData( GetWRefCon( theWindow ));
  210.     theWindow = frontUserWindow();
  211.     if( theWindow ) SetPort( theWindow );
  212.     }
  213.  
  214. doContent( theWindow, theEvent )
  215.     WindowPtr    theWindow;
  216.     EventRecord    *theEvent;
  217.     {
  218.     dataHandle    theDataHand;
  219.     dataPtr        theData;
  220.     Point            where;
  221.     int            whichCurs;
  222.     
  223.     theData = *( dataHandle )GetWRefCon( theWindow );
  224.     where.h = theEvent->where.h + theWindow->portBits.bounds.left;
  225.     where.v = theEvent->where.v + theWindow->portBits.bounds.top;
  226.     whichCurs = getToolCurs();
  227.     if( PtInRect( where, &( theData->dataRect )))
  228.         if( theEvent->modifiers & cmdKey ) {
  229.             forceCurs( marqeeCurs );
  230.             doSelect( theWindow, theEvent );
  231.         } else if( theData->isSelect && PtInRgn( where, theData->selRgn ) && 
  232.                 !( EqualRect( &( theData->dataRect ), &( theData->selBits.bounds )) 
  233.                 && whichCurs == marqeeCurs )) {
  234.             forceCurs( arrowCurs );
  235.             doSelDrag( theWindow, theEvent );
  236.         } else {
  237.             forceCurs( whichCurs );
  238.             switch( whichCurs ) {
  239.                 case pencilCurs:
  240.                     doBoxBrush( theWindow, theEvent, 1, 0 );
  241.                     break;
  242.                 case erasorCurs:
  243.                     doBoxBrush( theWindow, theEvent, 16, 1 );
  244.                     break;
  245.                 case elipseCurs:
  246.                 case    lineCurs:
  247.                 case rectCurs:
  248.                     doBoxedShape( theWindow, theEvent, whichCurs );
  249.                     break;
  250.                 case marqeeCurs:
  251.                     doSelect( theWindow, theEvent );
  252.                     break;
  253.                 default: ;
  254.             }
  255.         }
  256.     }
  257.  
  258. doUpdate( theWindow )
  259.     WindowPeek    theWindow;
  260.     {
  261.     dataHandle    theDataHand;
  262.     dataPtr        theData;
  263.     GrafPtr        savePort;
  264.     Rect            *bounds;
  265.     
  266.     GetPort( &savePort );
  267.     SetPort( theWindow );
  268.     theDataHand = ( dataHandle )GetWRefCon( theWindow );
  269.     HLock( theDataHand );
  270.     theData = *theDataHand;
  271.     fixAllBits( theData );
  272.     BeginUpdate( theWindow );
  273.     bounds = &( thePort->portRect );
  274.     CopyBig( &( theData->allBits ), &( thePort->portBits ), bounds, bounds, srcCopy, NULL );
  275.     EndUpdate( theWindow );
  276.       HUnlock( theDataHand );
  277.     SetPort( savePort );
  278.     }
  279.  
  280. doChangeTo( theWindow )
  281.     WindowPtr    theWindow;
  282.     {
  283.     SelectWindow( theWindow );
  284.     theWindow = frontUserWindow();
  285.     if( theWindow ) SetPort( theWindow );
  286.     }
  287.  
  288. /*******************************************************
  289. *                                                                *
  290. *                                                                *
  291. *******************************************************/
  292.  
  293. doSuspend( theEvent )
  294.     EventRecord    *theEvent;
  295. {
  296.     WindowPeek    theWindow;
  297.  
  298.     inBack = -1;
  299.     theWindow = (WindowPeek)FrontWindow();
  300.     if( theWindow )
  301.         if( theWindow->windowKind >=  userKind ) {
  302.             exportScrap();
  303.             doDeactivate( theWindow );
  304.         } else {
  305.             theEvent->what = activateEvt;
  306.             theEvent->message = (long)theWindow;
  307.             theEvent->modifiers &= deactiveMask;
  308.             SystemEvent( theEvent );
  309.         }
  310.     else exportScrap();
  311. }
  312.  
  313. doResume( theEvent )
  314.     EventRecord    *theEvent;
  315. {
  316.     WindowPeek    theWindow;
  317.     int            doClip;
  318.  
  319.     doClip = theEvent->message & clipFlag;
  320.     inBack = 0;
  321.     theWindow = (WindowPeek)FrontWindow();
  322.     if( theWindow )
  323.         if( theWindow->windowKind >= userKind ) {
  324.             setBadScrap();
  325.             doActivate( theWindow );
  326.         } else {
  327.             theEvent->what = activateEvt;
  328.             theEvent->message = (long)theWindow;
  329.             theEvent->modifiers |= activeFlag;
  330.             SystemEvent( theEvent );
  331.         }
  332.     else setBadScrap();
  333.     forceCurs( badCursFlag );
  334. }
  335.  
  336. activeAnts( theDataHand, newValue )
  337.     dataHandle    theDataHand;
  338.     int            newValue;
  339.     {
  340.     GrafPtr        savePort;
  341.     Rect            *bounds;
  342.     dataPtr        theData;
  343.     
  344.     HLock( theDataHand );
  345.     theData = *theDataHand;
  346.     if( theData->isSelect )
  347.         {
  348.         if( theData->selActive && !newValue ) --isBackground;
  349.         else if( !( theData->selActive ) && newValue ) ++isBackground;
  350.         theData->selActive = newValue;
  351.         theData->allBitsState |= bitsBadAnts;
  352.         GetPort( &savePort );
  353.         SetPort( theData->parent );
  354.         fixAllBits( theData );
  355.         bounds = &( theData->selBits.bounds );
  356.         CopyBig( &( theData->allBits ), &( thePort->portBits ), bounds, bounds, srcCopy, NULL );
  357.         SetPort( savePort );
  358.         }
  359.     }
  360.  
  361. doActivateEvt( theWindow )
  362.     WindowPeek    theWindow;
  363. {
  364.     WindowPeek    next;
  365.     
  366.     next = theWindow->nextWindow;
  367.     if( next && next->windowKind < userKind )
  368.         setBadScrap();
  369.     doActivate( theWindow );
  370. }
  371.  
  372. doActivate( theWindow )
  373.     WindowPtr    theWindow;
  374. {
  375.     SetPort( theWindow );
  376.     activeAnts( GetWRefCon( theWindow ), -1 );
  377. }
  378.  
  379. doDeactivateEvt( theWindow )
  380.     WindowPeek    theWindow;
  381. {
  382.     WindowPeek    front;
  383.     
  384.     front = ( WindowPeek )FrontWindow();
  385.     if( front->windowKind < userKind )
  386.         exportScrap();
  387.     doDeactivate( theWindow );
  388. }
  389.  
  390. doDeactivate( theWindow )
  391.     WindowPtr    theWindow;
  392.     {
  393.     activeAnts( GetWRefCon( theWindow ), 0 );
  394.     }
  395.  
  396. /*******************************************************
  397. *                                                                *
  398. *                                                                *
  399. *******************************************************/
  400.  
  401. makeDataWindow( theWindow, theFont, flags )
  402.     WindowPtr    theWindow;
  403.     dataFontInfo    *theFont;
  404.     int            flags;
  405. {
  406.     ClipRect( &( theWindow->portRect ));
  407.     TextFont( theFont->number );
  408.     TextSize( theFont->size );
  409.     TextFace( 0 );
  410.     TextMode( srcOr );
  411.     PenMode( patXor );
  412.     newData( theWindow, theFont, flags );
  413. }
  414.  
  415. doNew( flags )
  416.     int            flags;
  417.     dataFontInfo    theFont;
  418.     WindowPtr    theWindow;
  419.     char            title[ pasStrLen ];
  420.     Rect            bounds;
  421.     
  422.     getDataFontInfo( &theFont, statFont );
  423.     newCorner( &( bounds.left ), &( bounds.top ));
  424.     bounds.right = defaultDataWidth + bounds.left;
  425.     bounds.bottom = defaultDataHeight + bounds.top;
  426.     if( flags & dataStatLine ) bounds.bottom += theFont.topSpace;
  427.     GetIndString( title, windTitles, newWindTitle );
  428.     theWindow = NewWindow( NULL, &bounds, title, -1, documentProc, (long)-1, -1, NULL );
  429.     SetPort( theWindow );
  430.     makeDataWindow( theWindow, &theFont, flags );
  431. }
  432.  
  433. doFake( theWindow, flags )
  434.     WindowPtr    theWindow;
  435.     int            flags;
  436. {
  437.     GrafPtr        savePort;
  438.     dataFontInfo    theFont;
  439.     
  440.     GetPort( &savePort );
  441.     SetPort( theWindow );
  442.     getDataFontInfo( &theFont, statFont );
  443.     makeDataWindow( theWindow, &theFont, flags );
  444.     SetPort( savePort );
  445. }
  446.  
  447. doClose( theWindow )
  448.     WindowPeek    theWindow;
  449.     {
  450.     int             kind;
  451.     WindowPeek    next;
  452.  
  453.     kind = theWindow->windowKind;
  454.     next = theWindow->nextWindow;
  455.     if( kind >= userKind )
  456.         {
  457.         disposeData( GetWRefCon( theWindow ));
  458.         if( next && next->windowKind < userKind )
  459.             exportScrap();
  460.         DisposeWindow( theWindow );
  461.         }
  462.     else    {
  463.         if( !next || ( next && next->windowKind >= userKind ))
  464.             setBadScrap();        /* This may not be needed if the DeskAcc decides not to close... */
  465.         installModalTrap();
  466.         CloseDeskAcc( kind );
  467.         removeModalTrap();
  468.         }
  469.     theWindow = (WindowPeek)frontUserWindow();
  470.     if( theWindow ) SetPort( theWindow );
  471.     }
  472.  
  473. doAllClose()
  474.     {
  475.     WindowPeek    theWindow, next;
  476.  
  477.     theWindow = (WindowPeek)FrontWindow();
  478.     while( theWindow )
  479.         {
  480.         next = theWindow->nextWindow;
  481.         doClose( theWindow );
  482.         theWindow = next;
  483.         }
  484.     }